package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.DispatchOrder;
import cn.stylefeng.guns.modular.system.mapper.DispatchOrderMapper;
import cn.stylefeng.guns.modular.system.model.params.DispatchOrderParam;
import cn.stylefeng.guns.modular.system.model.result.DispatchOrderResult;
import  cn.stylefeng.guns.modular.system.service.DispatchOrderService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 发货表 服务实现类
 * </p>
 *
 * @author ZhangJun
 * @since 2019-07-04
 */
@Service
public class DispatchOrderServiceImpl extends ServiceImpl<DispatchOrderMapper, DispatchOrder> implements DispatchOrderService {

    @Override
    public void add(DispatchOrderParam param){
        DispatchOrder entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(DispatchOrderParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(DispatchOrderParam param){
        DispatchOrder oldEntity = getOldEntity(param);
        DispatchOrder newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DispatchOrderResult findBySpec(DispatchOrderParam param){
        return null;
    }

    @Override
    public List<DispatchOrderResult> findListBySpec(DispatchOrderParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DispatchOrderParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param, ShiroKit.getUserNotNull().getId());
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(DispatchOrderParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private DispatchOrder getOldEntity(DispatchOrderParam param) {
        return this.getById(getKey(param));
    }

    private DispatchOrder getEntity(DispatchOrderParam param) {
        DispatchOrder entity = new DispatchOrder();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
